<?php
/**
 */
class ExpressionsTable extends Doctrine_Table
{
	public function getAnonymousExpressions()
	{
		$query = new Doctrine_Query();
		$query->select('e.id,s.receiver_id')
		       ->from('Expressions e')
			   ->LeftJoin('e.ExpressionsSent s')
			   ->where("s.receiver_id='".$_SESSION['sess_userid']."'")
			   ->andWhere("e.anonymous ='1'")
   			   ->andWhere("e.show_anonymous ='no'");
			  //echo $query->getSql();exit;
		return $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		 
	        
	}
	public function getBlockIds()
	{
		$query = new Doctrine_Query();
		$query->select('m.ifrom_memid')
		       ->from('MemberStatus m')
			   ->where("m.ito_memid='".$_SESSION['sess_userid']."'")
			   ->andWhere("m.mem_status='block'");
			    //echo $query->getSql();exit;
		return $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		 
	        
	}
	public function getAnonymousExpressionsByUserId()
	{
		$query = new Doctrine_Query();
		$query->select('e.id,s.receiver_id')
		       ->from('Expressions e')
			   ->LeftJoin('e.ExpressionsSent s')
			   ->where("s.receiver_id='".$_SESSION['sess_userid']."'")
			   ->andWhere("e.anonymous ='1'")
   			   ->andWhere("e.show_anonymous ='yes'");
			  //echo $query->getSql();exit;
		return $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		 
	        
	}
	public function getDetails($expr_id)
	{
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id,i.email as email,s.sender_id as sender_id,s.status as status,a.attachments,f.status')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s ON e.id = s.expression_id')
			   ->leftJoin('e.MyFavorites f ON f.type_id = e.id')
			   ->leftJoin('e.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.expression_id='".$expr_id."'")
			   ->andWhere("s.status !='RDelete'")
   			   ->andWhere("s.status !='Delete'")
			   ->andwhere("s.sender_id NOT IN (".$blockList.")")
			   ->andWhere("e.send_type=1");
			   //echo $query->getSqlQuery();exit;
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	        
	}
	public function getsentDetails($expr_id,$send_type)
	{
		$query = new Doctrine_Query();
		$query->select("e.*,m.message_intensity as message_intensity,m.desc as desc,s.is_group,if(s.is_group =1,g.name,i.first_name) as name,if(s.is_group =1,'',u.photo) as photo,s.sender_id as sender_id,s.status as status,s.receiver_email,s.receiver_id,a.attachments")
		       ->from('Expressions e')
			   ->leftJoin('e.ExpressionsSent s ON e.id = s.expression_id')
			   ->leftJoin('s.UserDetails u ON s.receiver_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->leftJoin('s.Users i ON i.user_id=s.receiver_id')
			   ->leftJoin('s.Groups g ON g.group_id=s.receiver_id')
			   ->where("s.expression_id='".$expr_id."'")
			   ->andWhere("s.status !='SDelete'")
   			   ->andWhere("e.send_type=".$send_type)
			   //->groupBy('e.id')
			   ->orderBy("e.id desc");
			   //echo $query->getSql();;exit;
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	        
	}
	

	public function getTrashDetails($expr_id)
	{
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id as sender_id,s.status as status,a.attachments')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->leftJoin('e.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.expression_id='".$expr_id."'")
			   ->andWhere("s.status ='RDelete' OR s.status ='Sdelete'");
			   //->andWhere("e.send_type=1");
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	}
	public function getExpressionDetails($expr_id)
	{
		$query = new Doctrine_Query();
		$query->select('e.*')
		       ->from('Expressions e')
			   ->where("e.id='".$expr_id."'");
			   //->andWhere("e.send_type=1");
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	}
	public function getAnonymousDetails($expr_id)
	{
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id as sender_id,s.status as status,a.attachments as attachment')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->leftJoin('e.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.expression_id='".$expr_id."'")
			   ->andWhere("s.status !='RDeleted'")
			   ->andWhere("s.status !='Deleted'")
			   ->andWhere("e.anonymous='1'")
			   //->andWhere("e.status !='Deleted'")
			   ->andwhere("s.sender_id NOT IN (".$blockList.")")
			   ->andWhere("e.send_type=1");
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	}
	function getGroupIds($userId)
	{
		$query = new Doctrine_Query();
		$query->select('g.*')
		       ->from('GroupDetails g')
			   ->where('g.user_id ='.$userId);
		//echo $query->getSql();;exit;
		return $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
	}
	public function getExpressions($userId,$currentPage,$resultsPerPage,$serValue='')
	{
		//echo $serValue;exit;
		$groupIdsList = '';
		$groupIds = $this->getGroupIds($userId);
		foreach($groupIds as $groupIds)
		{
			$groupIdsList.=  $groupIds['group_id'].',';
		}
		$groupIdsList = trim($groupIdsList,',');
		if($groupIdsList=='')
		{
			$groupIdsList = 0;
		}
		$anonymousList = '';
		$expr_Ids = $this->getAnonymousExpressions();
		foreach($expr_Ids as $expr_Ids)
		{
			$anonymousList.=  $expr_Ids['id'].',';
		}
		$anonymousList = trim($anonymousList,',');
		if($anonymousList=='')
		{
			$anonymousList = 0;
		}
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		//echo $groupIdsList;exit;
		//echo '<pre>';print_r($anonymousList);exit;
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id,s.status,s.read_status,a.attachments as attachment,f.status')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s ON e.id = s.expression_id')
			   ->leftJoin('e.MyFavorites f ON f.type_id = e.id')
			   ->leftJoin('s.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('s.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.receiver_id='".$userId."' OR s.receiver_id IN (".$groupIdsList.")")
			   ->andWhere("e.id NOT IN (".$anonymousList.")")
			   //->where("if(s.is_group_id =1,s.receiver_id='".$userId."'","'s.receiver_id IN (".$groupIdsList."'")
			   ->andWhere("s.status !='RDelete'")
   			   ->andWhere("s.status !='Delete'")
			   ->andwhere("s.sender_id NOT IN (".$blockList.")")
			   ->andWhere("e.send_type=1")
			   //->groupBy('e.id')
			   ->orderBy("e.id desc");
			  // echo $serValue;exit;
				if($serValue!='0')
				{
					//echo $serValue;exit;
					$query->andWhere("e.message LIKE '%".$serValue."%' OR e.subject LIKE '%".$serValue."%' OR CONCAT(i.first_name,i.username) LIKE '%".$serValue."%' OR i.email LIKE '%".$serValue."%'");
					
				} else {
					//echo 'oops';exit;
				}
			//echo $query->getSqlQuery();exit;
			require_once('../database/models/Pager.php');
			$pager = new Pager($currentPage,$resultsPerPage);
			
			$result = $pager->pagination($query,$serValue);
			//echo '<pre>';print_r($result);exit;
			return $result;
	        
	}
	
///To get count fof recevied
	public function getExpressionsCurrent($userId,$currentPage)
	{
		//echo $serValue;exit;
		$groupIdsList = '';
		$groupIds = $this->getGroupIds($userId);
		foreach($groupIds as $groupIds)
		{
			$groupIdsList.=  $groupIds['group_id'].',';
		}
		$groupIdsList = trim($groupIdsList,',');
		if($groupIdsList=='')
		{
			$groupIdsList = 0;
		}
		$anonymousList = '';
		$expr_Ids = $this->getAnonymousExpressions();
		foreach($expr_Ids as $expr_Ids)
		{
			$anonymousList.=  $expr_Ids['id'].',';
		}
		$anonymousList = trim($anonymousList,',');
		if($anonymousList=='')
		{
			$anonymousList = 0;
		}
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		//$currentPage = 2;
		//echo $currentPage;exit;
		//echo '<pre>';print_r($anonymousList);exit;
		if($currentPage ==1 || $currentPage=='')
		{
			$limit = 0;
		} else {
			$limit = (($currentPage-1) * 12);
		}
		//echo $limit;exit;
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id,s.status,s.read_status,a.attachments as attachment,f.status')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s ON e.id = s.expression_id')
			   ->leftJoin('e.MyFavorites f ON f.type_id = e.id')
			   ->leftJoin('s.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('s.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.receiver_id='".$userId."' OR s.receiver_id IN (".$groupIdsList.")")
			   ->andWhere("e.id NOT IN (".$anonymousList.")")
			   //->where("if(s.is_group_id =1,s.receiver_id='".$userId."'","'s.receiver_id IN (".$groupIdsList."'")
			   ->andWhere("s.status !='RDelete'")
   			   ->andWhere("s.status !='Delete'")
			   ->andwhere("s.sender_id NOT IN (".$blockList.")")
			   ->andWhere("e.send_type=1")
			   ->orderBy("e.id desc Limit $limit,12")
			   ->groupBy("e.id");
			   //	->limit(2)
			   //->offset(1);
			//echo $query->getSqlQuery();exit;
			$result['result'] = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
			//echo '<pre>';print_r($result);exit;
			return $result;
	        
	}

//End
	
///To get count fof recevied
	public function getExpressionsCount($userId)
	{
		$groupIdsList = '';
		$groupIds = $this->getGroupIds($userId);
		foreach($groupIds as $groupIds)
		{
			$groupIdsList.=  $groupIds['group_id'].',';
		}
		$groupIdsList = trim($groupIdsList,',');
		if($groupIdsList=='')
		{
			$groupIdsList = 0;
		}
		$anonymousList = '';
		$expr_Ids = $this->getAnonymousExpressions();
		foreach($expr_Ids as $expr_Ids)
		{
			$anonymousList.=  $expr_Ids['id'].',';
		}
		$anonymousList = trim($anonymousList,',');
		if($anonymousList=='')
		{
			$anonymousList = 0;
		}
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		//echo $groupIdsList;exit;
		//echo '<pre>';print_r($anonymousList);exit;
		$query = new Doctrine_Query();
		$query->select('e.id,s.expression_id')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->where("s.receiver_id='".$userId."' OR s.receiver_id IN (".$groupIdsList.")")
			   ->andWhere("e.id NOT IN (".$anonymousList.")")
			   ->andWhere("s.status !='RDelete'")
   			   ->andWhere("s.status !='Delete'")
			   ->andwhere("s.sender_id NOT IN (".$blockList.")")
			   ->andWhere("e.send_type=1")
			   ->orderBy("e.id desc");
				//echo $query->getSqlQuery();exit;
			$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
			return $result;
	        
	}

//End

	
	public function trashMails($userId,$currentPage,$resultsPerPage)
	{
		$groupIdsList="";
		$groupIds = $this->getGroupIds($userId);
		foreach($groupIds as $groupIds)
		{
			$groupIdsList.=  $groupIds['group_id'].',';
		}
		$groupIdsList = trim($groupIdsList,',');
		if($groupIdsList=='')
		{
			$groupIdsList = 0;
		}
		$anonymousList = '';
		$expr_Ids = $this->getAnonymousExpressions();
		foreach($expr_Ids as $expr_Ids)
		{
			$anonymousList.=  $expr_Ids['id'].',';
		}
		$anonymousList = trim($anonymousList,',');
		if($anonymousList=='')
		{
			$anonymousList = 0;
		}
		//echo $groupIdsList;exit;
		//echo '<pre>';print_r($groupIds);exit;
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,s.read_status,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id,s.status,a.attachments as attachment')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->leftJoin('s.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('s.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.receiver_id='".$userId."' OR s.receiver_id IN (".$groupIdsList.") OR  s.sender_id='".$userId."'")
			   ->andWhere("e.id NOT IN (".$anonymousList.")")
			   //->where("if(s.is_group_id =1,s.receiver_id='".$userId."'","'s.receiver_id IN (".$groupIdsList."'")
			   //->andWhere("s.status ='RDelete'")
			   ->andWhere("s.status ='RDelete' OR s.status ='Sdelete'")
   			   //->andWhere("e.send_type=1")
			   //->groupBy('e.id')
			   ->orderBy("e.id desc");
				//echo $query->getSql();exit;
			require_once('../database/models/Pager.php');
			$pager = new Pager($currentPage,$resultsPerPage);
			//echo $query->getSql();
			$result = $pager->pagination($query);
			//echo '<pre>';print_r($result);exit;
			return $result;
	        
	}
	//////// function for both sentexpressions and draftssssss#####################################33
	public function sentMails($userId,$type,$currentPage,$resultsPerPage)
	{
		$query = new Doctrine_Query();
		$query->select("e.*,m.message_intensity as message_intensity,m.desc as desc,if(s.is_group =1,g.name,i.first_name) as name,if(s.is_group =1,'',u.photo) as photo,s.sender_id as sender_id,s.status as status,s.receiver_email,s.read_status,s.receiver_id,a.attachments as attachment")
		       ->from('Expressions e')
			   ->leftJoin('e.ExpressionsSent s ON e.id = s.expression_id')
			   ->leftJoin('s.UserDetails u ON s.receiver_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->leftJoin('s.Users i ON i.user_id=s.receiver_id')
			   ->leftJoin('s.Groups g ON g.group_id=s.receiver_id')
			   ->where("s.sender_id='".$userId."'")
			   ->andWhere("s.status !='SDelete'")
			   ->andWhere("s.status !='Delete'")
   			   ->andWhere("e.send_type=".$type)
			  // ->groupBy('e.id')
			   ->orderBy("e.id desc");
				//echo $query->getSql();exit;
			require_once('../database/models/Pager.php');
			$pager = new Pager($currentPage,$resultsPerPage);
			//echo $query->getSql();
			$result = $pager->pagination($query);
			//echo '<pre>';print_r($result);exit;
			return $result;
	        
	}
	
	public function sentMailsCount($userId,$type)
	{
		$query = new Doctrine_Query();
		$query->select("e.id,s.receiver_email,s.receiver_id")
		       ->from('Expressions e')
			   ->leftJoin('e.ExpressionsSent s ON e.id = s.expression_id')
			   ->where("s.sender_id='".$userId."'")
			   ->andWhere("s.status !='SDelete'")
   			   ->andWhere("e.send_type=".$type)
			  // ->groupBy('e.id')
			   ->orderBy("e.id desc");
				//echo $query->getSql();exit;
				$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
			return $result;
	        
	}
	
	public function getAnonymousMails($userId,$currentPage,$resultsPerPage)
	{
		$groupIds = $this->getGroupIds($userId);
		foreach($groupIds as $groupIds)
		{
			$groupIdsList.=  $groupIds['group_id'].',';
		}
		$groupIdsList = trim($groupIdsList,',');
		if($groupIdsList=='')
		{
			$groupIdsList = 0;
		}
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		//echo $groupIdsList;exit;
		//echo '<pre>';print_r($groupIds);exit;
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,s.read_status,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id,s.status,a.attachments as attachment')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->leftJoin('s.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('s.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.receiver_id='".$userId."' OR s.receiver_id IN (".$groupIdsList.")")
			   //->where("if(s.is_group_id =1,s.receiver_id='".$userId."'","'s.receiver_id IN (".$groupIdsList."'")
			   ->where("s.receiver_id='".$userId."'")
			   ->andWhere("s.status !='RDelete'")
			   ->andWhere("s.status !='Delete'")
			   ->andWhere("e.anonymous='1'")
			   //->andWhere("e.status !='Deleted'")
			   ->andwhere("s.sender_id NOT IN (".$blockList.")")
			   ->andWhere("e.send_type=1")
			   //->groupBy('e.id')
			   ->orderBy("e.id desc");
				//echo $query->getSql();exit;
			require_once('../database/models/Pager.php');
			$pager = new Pager($currentPage,$resultsPerPage);
			//echo $query->getSql();
			$result = $pager->pagination($query);
			//echo '<pre>';print_r($result);exit;
			return $result;
	        
	}
	public function getBlockedDetails($expr_id)
	{
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id as sender_id,s.status as status,a.attachments as attachment')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->leftJoin('e.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.expression_id='".$expr_id."'")
			   ->andwhere("s.sender_id IN (".$blockList.")")
			   ->andWhere("e.send_type=1");
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	        
	}
	public function getSentColors($userid,$my_colors)
	{
		$query = new Doctrine_Query();
		$query->select("e.id,s.receiver_email,s.receiver_id")
		       ->from('Expressions e')
			   ->leftJoin('e.ExpressionsSent s ON e.id = s.expression_id')
			   ->where("s.sender_id='".$userid."'")
			   ->andWhere("s.status !='SDelete'")
			   ->andWhere("s.status !='Delete'")
   			   ->andWhere("e.send_type='1'")
			   ->andWhere("e.message_color_id='".$my_colors."'")
			  // ->groupBy('e.id')
			   ->orderBy("e.id desc");
				//echo $query->getSql();exit;
				$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
			return $result;
		
	}
	public function getReceivedColors($userid,$my_colors)
	{		
		$groupIdsList = '';
		$groupIds = $this->getGroupIds($userid);
		foreach($groupIds as $groupIds)
		{
			$groupIdsList.=  $groupIds['group_id'].',';
		}
		$groupIdsList = trim($groupIdsList,',');
		if($groupIdsList=='')
		{
			$groupIdsList = 0;
		}
		$anonymousList = '';
		$expr_Ids = $this->getAnonymousExpressions();
		foreach($expr_Ids as $expr_Ids)
		{
			$anonymousList.=  $expr_Ids['id'].',';
		}
		$anonymousList = trim($anonymousList,',');
		if($anonymousList=='')
		{
			$anonymousList = 0;
		}
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		//echo $groupIdsList;exit;
		//echo '<pre>';print_r($anonymousList);exit;
		$query = new Doctrine_Query();
		$query->select('e.id,s.expression_id')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->where("s.receiver_id='".$userid."' OR s.receiver_id IN (".$groupIdsList.")")
			   ->andWhere("e.id NOT IN (".$anonymousList.")")
			   ->andWhere("s.status !='RDelete'")
   			   ->andWhere("s.status !='Delete'")
			   ->andwhere("s.sender_id NOT IN (".$blockList.")")
			   ->andWhere("e.send_type=1")
			   ->andWhere("e.message_color_id='".$my_colors."'")
			   ->orderBy("e.id desc");
				//echo $query->getSqlQuery();exit;
			$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
			return $result;
	}
	public function getReceivedMails($userid)
	{
			$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
			$query=new Doctrine_Query();
		    $query->select('count(e.expression_id) as numCount')
				  ->from('Expressions e')
			      ->innerJoin('e.ExpressionsSent s')
			      ->where("s.receiver_id='".$userid."'")
			      ->andWhere("s.status !='RDeleted'")
				  ->andWhere("s.status !='Deleted'")
				  ->andwhere("s.sender_id NOT IN (".$blockList.")")
			      ->andWhere("e.send_type='1'");
			   //->andWhere("e.status!='Deleted'");
			  // ->andWhere("e.anonymous='0'");
			   //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result[0];
				
		
	}
	public function getSentMails($userid)
	{
			$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
			$query=new Doctrine_Query();
		    $query->select('count(s.sender_id) as numCount')
		       ->from('Expressions e')
			   ->innerJoin('e.ExpressionsSent s')
			   ->where("s.sender_id='".$userid."'")
			   ->andWhere("e.send_type='1'")
			   ->andWhere("e.status!='SDeleted'")
			   ->andWhere("s.status !='Deleted'")
			   ->andwhere("s.sender_id NOT IN (".$blockList.")");
			   //->andWhere("e.status!='Deleted'");
			   //->andWhere("e.anonymous='0'");
			  // echo $query;exit;
			   	$result = $query->execute();
				//echo '<pre>';print_r($result);exit;
				return $result[0];
				
		
	}
	 public function getMails($memid)
	 {
		$query=new Doctrine_Query();
		    $query->select('DISTINCT(s.receiver_email) as email')
		       ->from('Expressions e')
			   ->where("s.sender_id='".$memid."'");
			     //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	 }
	public function addExpressions($data,$anonymous)
	{
		$subject = ucfirst(addslashes(trim($data['subject'])));
		$color_id = ucfirst(addslashes(trim($data['color_id'])));
		$intensity = ucfirst(addslashes(trim($data['intensity'])));
		$message = addslashes(trim($data['message']));
				
		$Expressions = new Expressions();
		$Expressions ->subject = $subject;
		$Expressions ->message_color_id = $color_id;
		$Expressions ->message_intensity = $intensity;
		$Expressions ->message = $message;
		$Expressions ->send_type = $data['sendType'];
		$Expressions ->create_time = date('Y-m-d H:i:s');
		$Expressions ->anonymous = $anonymous;
		
        //echo '<pre>';print_r($Expressions);exit;
		$Expressions ->save();
		return $Expressions->id;
	}
	public function changeSendTypeExpression($expr_id,$data)
	{
		 $query = new Doctrine_Query();
		$query->update('Expressions e')
			  ->set('e.send_type',"'$data[sendType]'")
			  ->set('e.message',"'$data[message]'")
			  ->set('e.subject',"'$data[subject]'")
			  ->where("e.id='".$expr_id."'");
		  //echo $query;exit;
		return $query->execute();
	}
	public function changeShowAnonymousStatus($expr_id,$show_anonymous)
	{
		 $query = new Doctrine_Query();
		$query->update('Expressions e')
			  ->set('e.show_anonymous',"'$show_anonymous'")
			  ->where("e.id='".$expr_id."'");
		  //echo $query;exit;
		return $query->execute();
	}
	public function getBlockedExpressions($userId,$currentPage,$resultsPerPage)
	{
		$groupIds = $this->getGroupIds($userId);
		foreach($groupIds as $groupIds)
		{
			$groupIdsList.=  $groupIds['group_id'].',';
		}
		$groupIdsList = trim($groupIdsList,',');
		if($groupIdsList=='')
		{
			$groupIdsList = 0;
		}
			$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		//echo $groupIdsList;exit;
		//echo '<pre>';print_r($groupIds);exit;
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id,s.status,s.read_status,a.attachments as attachment')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->leftJoin('s.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('s.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.receiver_id='".$userId."' OR s.receiver_id IN (".$groupIdsList.")")
			   //->where("if(s.is_group_id =1,s.receiver_id='".$userId."'","'s.receiver_id IN (".$groupIdsList."'")
			   ->andwhere("s.sender_id IN (".$blockList.")")
			   ->andWhere("e.send_type=1")
			   ->andWhere("s.status!='Rdelete'")
			   //->groupBy('e.id')
			   ->orderBy("e.id desc");
				//echo $query->getSql();exit;
			require_once('../database/models/Pager.php');
			$pager = new Pager($currentPage,$resultsPerPage);
			//echo $query->getSql();
			$result = $pager->pagination($query);
			//echo '<pre>';print_r($result);exit;
			return $result;
	        
	        
	}
	/////////////////////////////// checking
	public function getBlockedById($rece_id,$blocked_id)
	{
		$blockList = '';
		$blockids = $this->getBlockIds();
		foreach($blockids as $blockids)
		{
			$blockList.=  $blockids['ifrom_memid'].',';
		}
		$blockList = trim($blockList,',');
		if($blockList=='')
		{
			$blockList = 0;
		}
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id,s.sender_id as sender_id,s.status as status,a.attachments as attachment')
		       ->from('Expressions e')
			   ->InnerJoin('e.ExpressionsSent s')
			   ->leftJoin('e.UserDetails u ON s.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.Users i ON i.user_id=s.sender_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->where("s.receiver_id='".$rece_id."'")
			   ->andWhere("s.sender_id='".$blocked_id."'")
			   ->andwhere("s.sender_id IN (".$blockList.")")
			   ->andWhere("e.send_type=1")
			   ->groupBy('e.id')
			   ->orderBy("e.id desc");
				//echo $query->getSql();exit;
			require_once('../database/models/Pager.php');
			$pager = new Pager($currentPage,$resultsPerPage);
			//echo $query->getSql();
			$result = $pager->pagination($query);
			//echo '<pre>';print_r($result);exit;
			return $result[0];
	        
	}
	///show list in view pages#########################3
	public function sentMailsCurrent($userId,$type,$currentPage)
	{
		if($currentPage ==1 || $currentPage=='')
		{
			$limit = 0;
		} else {
			$limit = (($currentPage-1) * 12);
		}
		$query = new Doctrine_Query();
		$query->select("e.*,m.message_intensity as message_intensity,s.read_status,m.desc as desc,if(s.is_group =1,g.name,i.first_name) as name,if(s.is_group =1,'',u.photo) as photo,s.sender_id as sender_id,s.status as status,s.receiver_email,s.receiver_id,a.attachments as attachment")
		       ->from('Expressions e')
			   ->leftJoin('e.ExpressionsSent s ON e.id = s.expression_id')
			   ->leftJoin('s.UserDetails u ON s.receiver_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.ExpressionsAttachements a on e.id=a.expression_id')
			   ->leftJoin('s.Users i ON i.user_id=s.receiver_id')
			   ->leftJoin('s.Groups g ON g.group_id=s.receiver_id')
			   ->where("s.sender_id='".$userId."'")
			   ->andWhere("s.status !='SDelete'")
			   ->andWhere("s.status !='Delete'")
   			   ->andWhere("e.send_type=".$type)
			  // ->groupBy('e.id')
			   ->orderBy("e.id desc Limit $limit,12")
			   ->groupBy("e.id");
			   //echo $query->getSqlQuery();exit;
			$result['result'] = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	        
	}
}